package com.ruoyi.garbage.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.garbage.domain.KeywordResult;
import com.ruoyi.garbage.domain.KeywordSearchNum;
import com.ruoyi.garbage.domain.QuestionBank;
import com.ruoyi.garbage.mapper.KeywordResultMapper;
import com.ruoyi.garbage.mapper.KeywordSearchNumMapper;
import com.ruoyi.garbage.mapper.QuestionBankMapper;
import com.ruoyi.garbage.service.IQuestionBankService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 题库Service业务层处理
 *
 * @author 刘哲贤
 * @date 2023-01-07
 */
@Service
public class QuestionBankServiceImpl implements IQuestionBankService {
    @Resource
    private QuestionBankMapper questionBankMapper;
    @Resource
    private KeywordResultMapper keywordResultMapper;
    @Resource
    private KeywordSearchNumMapper keywordSearchNumMapper;

    /**
     * 查询题库
     *
     * @param questionId 题库主键
     * @return 题库
     */
    @Override
    public QuestionBank selectQuestionBankByQuestionId(Long questionId) {
        return questionBankMapper.selectQuestionBankByQuestionId(questionId);
    }

    /**
     * 查询题库列表
     *
     * @param questionBank 题库
     * @return 题库
     */
    @Override
    public List<QuestionBank> selectQuestionBankList(QuestionBank questionBank) {
        return questionBankMapper.selectQuestionBankList(questionBank);
    }

    /**
     * 新增题库
     *
     * @param questionBank 题库
     * @return 结果
     */
    @Override
    public int insertQuestionBank(QuestionBank questionBank) {
        return questionBankMapper.insertQuestionBank(questionBank);
    }

    /**
     * 修改题库
     *
     * @param questionBank 题库
     * @return 结果
     */
    @Override
    public int updateQuestionBank(QuestionBank questionBank) {
        return questionBankMapper.updateQuestionBank(questionBank);
    }

    /**
     * 批量删除题库
     *
     * @param questionIds 需要删除的题库主键
     * @return 结果
     */
    @Override
    public int deleteQuestionBankByQuestionIds(Long[] questionIds) {
        return questionBankMapper.deleteQuestionBankByQuestionIds(questionIds);
    }

    /**
     * 删除题库信息
     *
     * @param questionId 题库主键
     * @return 结果
     */
    @Override
    public int deleteQuestionBankByQuestionId(Long questionId) {
        return questionBankMapper.deleteQuestionBankByQuestionId(questionId);
    }

    @Override
    public Map<String, Object> searchQuestionByUniName(String name) {
        if (!StringUtils.hasLength(name)) {
            return null;
        }
        // 根据name查询题目
        QuestionBank questionBank = questionBankMapper.selectByName(name);

        // 题库查询条件
        QuestionBank query = new QuestionBank();
        query.setGarbageName(name);
        List<QuestionBank> list = questionBankMapper.selectQuestionBankList(query);
        KeywordResult keywordResult = new KeywordResult();
        keywordResult.setKeyword(name);
        keywordResult.setResult(JSONObject.toJSONString(list));
        keywordResultMapper.insertKeywordResult(keywordResult);
        KeywordSearchNum keywordNum = keywordSearchNumMapper.selectByKeyword(name);
        if (keywordNum != null) {
            keywordNum.setNum(keywordNum.getNum() + 1);
            keywordSearchNumMapper.updateKeywordSearchNum(keywordNum);
        } else {
            keywordNum = new KeywordSearchNum();
            keywordNum.setKeyword(name);
            keywordNum.setNum(1);
            keywordNum.setTimes(new Date());
            keywordSearchNumMapper.insertKeywordSearchNum(keywordNum);
        }
        HashMap<String, Object> map = new HashMap<>(32);
        map.put("uni", questionBank);
        map.put("results", list);
        return map;
    }
}
